package willevaluate.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import view.MyConfirmDialog;

/**
 * Created by Administrator on 2017/6/9.
 * 修改订单
 */
@ContentView(R.layout.expert_evaluate_reset_order)
public class ExpertEvaluateResetOrder extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.common_title)
    private TextView common_title;
    @ViewInject(R.id.expert_evaluate_reset_order_item11)
    private TextView expert_evaluate_reset_order_item11;
    @ViewInject(R.id.expert_evaluate_reset_order_item12)
    private TextView expert_evaluate_reset_order_item12;
    @ViewInject(R.id.expert_evaluate_reset_order_item21)
    private TextView expert_evaluate_reset_order_item21;
    @ViewInject(R.id.expert_evaluate_reset_order_item22)
    private TextView expert_evaluate_reset_order_item22;
    @ViewInject(R.id.expert_evaluate_reset_order_other)
    private EditText expert_evaluate_reset_order_other;
    @ViewInject(R.id.expert_evaluate_reset_order_commit)
    private Button expert_evaluate_reset_order_commit;

    private String orderid;
    private List<String> feedbackList;//选项
    private Drawable unchekck;
    private Drawable chekcked;


    private MyHandler1 handler;

    static class MyHandler1 extends Handler {
        WeakReference<ExpertEvaluateResetOrder> weakReference;

        public MyHandler1(ExpertEvaluateResetOrder weakReference) {
            this.weakReference = new WeakReference<>(weakReference);
        }

        @Override
        public void handleMessage(Message msg) {
            ExpertEvaluateResetOrder expertEvaluateResetOrder = weakReference.get();
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS:
                    try {
                        JSONObject jsonObject = new JSONObject(String.valueOf(msg.obj));
                        String err_code = jsonObject.getString("err_code");
                        if("0".equals(err_code)){
                            expertEvaluateResetOrder.finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    break;
            }

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        orderid = getIntent().getStringExtra("orderid");
        handler = new MyHandler1(this);
        feedbackList = new ArrayList<>();
        initView();
    }

    private void initView() {
        common_back.setOnClickListener(this);
        common_title.setText("申请修改志愿");
        expert_evaluate_reset_order_commit.setOnClickListener(this);
        expert_evaluate_reset_order_item11.setOnClickListener(this);
        expert_evaluate_reset_order_item12.setOnClickListener(this);
        expert_evaluate_reset_order_item21.setOnClickListener(this);
        expert_evaluate_reset_order_item22.setOnClickListener(this);
        unchekck =getResources().getDrawable(R.drawable.questionnaire_unckeck);
        chekcked =getResources().getDrawable(R.drawable.questionnaire_checked);
        unchekck.setBounds(0, 0, unchekck.getMinimumWidth(), unchekck.getMinimumHeight());
        chekcked.setBounds(0, 0, chekcked.getMinimumWidth(), chekcked.getMinimumHeight());
    }

    @Override
    public void onBackPressed() {
        confirm();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                confirm();
                break;
            case R.id.expert_evaluate_reset_order_commit:
                commit();
                break;
            case R.id.expert_evaluate_reset_order_item11:
                if(feedbackList.contains(expert_evaluate_reset_order_item11.getText().toString())){
                    feedbackList.remove(expert_evaluate_reset_order_item11.getText().toString());
                    expert_evaluate_reset_order_item11.setCompoundDrawables(unchekck,null,null,null);
                }else{
                    feedbackList.add(expert_evaluate_reset_order_item11.getText().toString());
                    expert_evaluate_reset_order_item11.setCompoundDrawables(chekcked,null,null,null);
                }
                break;
            case R.id.expert_evaluate_reset_order_item12:
                if(feedbackList.contains(expert_evaluate_reset_order_item12.getText().toString())){
                    feedbackList.remove(expert_evaluate_reset_order_item12.getText().toString());
                    expert_evaluate_reset_order_item12.setCompoundDrawables(unchekck,null,null,null);
                }else{
                    feedbackList.add(expert_evaluate_reset_order_item12.getText().toString());
                    expert_evaluate_reset_order_item12.setCompoundDrawables(chekcked,null,null,null);
                }
                break;
            case R.id.expert_evaluate_reset_order_item21:
                if(feedbackList.contains(expert_evaluate_reset_order_item21.getText().toString())){
                    feedbackList.remove(expert_evaluate_reset_order_item21.getText().toString());
                    expert_evaluate_reset_order_item21.setCompoundDrawables(unchekck,null,null,null);
                }else{
                    feedbackList.add(expert_evaluate_reset_order_item21.getText().toString());
                    expert_evaluate_reset_order_item21.setCompoundDrawables(chekcked,null,null,null);
                }
                break;
            case R.id.expert_evaluate_reset_order_item22:
                if(feedbackList.contains(expert_evaluate_reset_order_item22.getText().toString())){
                    feedbackList.remove(expert_evaluate_reset_order_item22.getText().toString());
                    expert_evaluate_reset_order_item22.setCompoundDrawables(unchekck,null,null,null);
                }else{
                    feedbackList.add(expert_evaluate_reset_order_item22.getText().toString());
                    expert_evaluate_reset_order_item22.setCompoundDrawables(chekcked,null,null,null);
                }
                break;
            default:
                break;
        }
    }

    private void confirm() {
        MyConfirmDialog myConfirmDialog = new MyConfirmDialog(this,"退出将不保存信息","是","否");
        myConfirmDialog.setConfirmListener(new MyConfirmDialog.ConfirmListener() {
            @Override
            public void confirm() {
                ExpertEvaluateResetOrder.this.finish();
            }
        });
        myConfirmDialog.show();
    }


    private void commit() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("orderid",orderid);
        hashMap.put("content",expert_evaluate_reset_order_other.getText().toString().trim());
        StringBuffer feedback = new StringBuffer("");
        for (int i = 0; i < feedbackList.size(); i++) {
            if(i!=(feedbackList.size()-1)) {
                feedback.append(feedbackList.get(i) + ";");
            }else{
                feedback.append(feedbackList.get(i));
            }
        }
        hashMap.put("feedback",feedback.toString());
        new InterNetController(this, Constant.CHANGEORDER, handler, hashMap, 0);
    }
}
